import fetch from '../utils/fetch';

////////////////////////////////网关API//////////////////////////////////////
// 获取网关数据列表
export function ApiOfGatewayList() {
  return fetch({
    url: '/core/api/w/gateways',
    method: 'get',
  })
}
export const ApiOfGatewayPage = '/core/api/w/gateways/p'
export const ApiOfGatewayEnable = (gatewayCode) => {
  return fetch({
    url: '/core/api/w/gateways/' + gatewayCode + '/enable',
    method: 'put',
  })
}
export const ApiOfGatewayDisable = (gatewayCode) => {
  return fetch({
    url: '/core/api/w/gateways/' + gatewayCode + '/disable',
    method: 'put',
  })
}
export const ApiOfGatewayDetail = (code) => {
  return fetch({
    url: '/core/api/w/gateways/' + code,
    method: 'get',
  })
}
export const ApiOfGatewayCreate = (form) => {
  return fetch({
    url: '/core/api/w/gateways',
    method: 'post',
    data: form,
  })
}
export const ApiOfGatewayUpdate = (gatewayCode, form) => {
  return fetch({
    url: '/core/api/w/gateways/' + gatewayCode,
    method: 'put',
    data: form,
  })
}
export const ApiOfGatewayDelete = (code) => {
  return fetch({
    url: '/core/api/w/gateways/' + code,
    method: 'delete',
  })
}
export const ApiOfGatewayImport = '/databus/core/api/w/gateways/import'
export const ApiOfGatewayTemplate = '/databus/core/api/w/gateways/template'
export const ApiOfGatewayExport = '/databus/core/api/w/gateways/export'
export const ApiOfGatewayBatchEnable = (gatewayCode) => {
  return fetch({
    url: '/core/api/w/gateways/' + gatewayCode + '/batch/enable',
    method: 'put',
  })
}
export const ApiOfGatewayBatchDisable = (gatewayCode) => {
  return fetch({
    url: '/core/api/w/gateways/' + gatewayCode + '/batch/disable',
    method: 'put',
  })
}

////////////////////////////////资源API//////////////////////////////////////
export function ApiOfResourceList(params) {
  return fetch({
    url: '/core/api/w/resources',
    method: 'get',
    params,
  })
}
export const ApiOfResourceGet = (code) => {
  return fetch({
    url: '/core/api/w/resources/' + code,
    method: 'get',
  })
}
export const ApiOfResourcePage = '/core/api/w/resources/p'
export const ApiOfResourceEnable = (code) => {
  return fetch({
    url: '/core/api/w/resources/' + code + '/enable',
    method: 'put',
  })
}
export const ApiOfResourceDisable = (code) => {
  return fetch({
    url: '/core/api/w/resources/' + code + '/disable',
    method: 'put',
  })
}
export const ApiOfResourceCreate = (form) => {
  return fetch({
    url: '/core/api/w/resources',
    method: 'post',
    data: form,
  })
}
export const ApiOfResourceUpdate = (code, form) => {
  return fetch({
    url: '/core/api/w/resources/' + code,
    method: 'put',
    data: form,
  })
}
export const ApiOfResourceDelete = (code) => {
  return fetch({
    url: '/core/api/w/resources/' + code,
    method: 'delete',
  })
}
export const ApiOfResourceBindMetricGroups = (code, metrics) => {
  return fetch({
    url: '/core/api/w/resources/' + code + '/bind',
    method: 'put',
    data: metrics,
  })
}
export const ApiOfCollectTest = (code) => {
  return fetch({
    url: '/core/api/w/resources/' + code + '/collect',
    method: 'get',
  })
}

export const ApiOfControlTest = (code, metric) => {
  return fetch({
    url: '/core/api/w/resources/' + code + '/control/' + metric,
    method: 'put',
  })
}
export const ApiOfResourceDistribution = () => {
  return fetch({
    url: '/core/api/w/resources/distribution',
    method: 'get',
  })
}
export const ApiOfResourceImport = '/databus/core/api/w/resources/ex/import'
export const ApiOfResourceTemplate = '/databus/core/api/w/resources/ex/template'
export const ApiOfResourceExport = '/databus/core/api/w/resources/ex/export'
////////////////////////////////指标组API//////////////////////////////////////
export function ApiOfMetricGroupList(params) {
  return fetch({
    url: '/core/api/w/metricgroups',
    method: 'get',
    params,
  })
}
export const ApiOfMetricGroupGet = (code) => {
  return fetch({
    url: '/core/api/w/metricgroups/' + code,
    method: 'get',
  })
}
export const ApiOfMetricGroupPage = '/core/api/w/metricgroups/p'
export const ApiOfMetricGroupEnable = (code) => {
  return fetch({
    url: '/core/api/w/metricgroups/' + code + '/enable',
    method: 'put',
  })
}
export const ApiOfMetricGroupDisable = (code) => {
  return fetch({
    url: '/core/api/w/metricgroups/' + code + '/disable',
    method: 'put',
  })
}
export const ApiOfMetricGroupCreate = (form) => {
  return fetch({
    url: '/core/api/w/metricgroups',
    method: 'post',
    data: form,
  })
}
export const ApiOfMetricGroupUpdate = (code, form) => {
  return fetch({
    url: '/core/api/w/metricgroups/' + code,
    method: 'put',
    data: form,
  })
}
export const ApiOfMetricGroupBindMetrics = (code, metrics) => {
  return fetch({
    url: '/core/api/w/metricgroups/' + code + '/bind',
    method: 'put',
    data: metrics,
  })
}
export const ApiOfMetricGroupDelete = (code) => {
  return fetch({
    url: '/core/api/w/metricgroups/' + code,
    method: 'delete',
  })
}
export const ApiOfMetricGroupExport = '/core/api/w/metricgroups/export'
////////////////////////////////指标API//////////////////////////////////////
export function ApiOfMetricList(searchable) {
  return fetch({
    url: '/core/api/w/metrics',
    method: 'get',
    params: searchable,
  })
}
export const ApiOfMetricPage = '/core/api/w/metrics/p'
export const ApiOfMetricEnable = (code) => {
  return fetch({
    url: '/core/api/w/metrics/' + code + '/enable',
    method: 'put',
  })
}
export const ApiOfMetricDisable = (code) => {
  return fetch({
    url: '/core/api/w/metrics/' + code + '/disable',
    method: 'put',
  })
}
export const ApiOfMetricCreate = (form) => {
  return fetch({
    url: '/core/api/w/metrics',
    method: 'post',
    data: form,
  })
}
export const ApiOfMetricUpdate = (code, form) => {
  return fetch({
    url: '/core/api/w/metrics/' + code,
    method: 'put',
    data: form,
  })
}
export const ApiOfMetricDelete = (code) => {
  return fetch({
    url: '/core/api/w/metrics/' + code,
    method: 'delete',
  })
}
export const ApiOfMetricDeleteBatch = (data) => {
  return fetch({
    url: '/core/api/w/metrics/batch',
    method: 'delete',
    data,
  })
}
export const ApiOfMetricImport = '/databus/core/api/w/metrics/import'
export const ApiOfMetricTemplate = '/databus/core/api/w/metrics/template'

////////////////////////////////监控数据API//////////////////////////////////////
export const ApiOfMonitorDataPage = '/core/api/w/monitordata/p'

export const ApiOfRealtimeMonitorData = (resourceCode) => {
  return fetch({
    url: '/core/api/w/monitordata/realtime/' + resourceCode,
    method: 'get',
    ignoreError: true,
  })
}

export const UrlOfRealtimeMonitorData = (resourceCode) => {
  return '/core/api/w/monitordata/realtime/' + resourceCode
}

export const ApiOfMonitorDayCount = (resourceCode, day) => {
  return fetch({
    url: '/core/api/w/monitordata/statistics/'+ resourceCode +'/' + day,
    method: 'get',
    ignoreError: true,
  })
}

export const ApiOfLatestMonitorData = (resourceCode, latestLimit, qualifier) => {
  return fetch({
    url: '/core/api/w/monitordata/'+ resourceCode,
    method: 'get',
    ignoreError: true,
    params: {
      s_latestLimit: latestLimit,
      s_qualifier: qualifier
    }
  })
}

////////////////////////////////任务API//////////////////////////////////////
export const ApiOfJobPage = '/core/schedule/triggers'
export const ApiOfJobPause = (gruop, name) => {
  return fetch({
    url: '/core/schedule/' + gruop + '/' + name + '/pauseJob',
    method: 'put',
  })
}
export const ApiOfJobResume = (gruop, name) => {
  return fetch({
    url: '/core/schedule/' + gruop + '/' + name + '/resumeJob',
    method: 'put',
  })
}
export const ApiOfJobDelete = (gruop, name) => {
  return fetch({
    url: '/core/schedule/' + gruop + '/' + name + '/deleteJob',
    method: 'delete',
  })
}
export const ApiOfJobRoutes = (gruop, name) => {
  return '/core/api/w/jobs/routes/' + gruop + '/' + name;
}
export const ApiOfJobSwitchRoute = (jobKey, to) => {
  return fetch({
    url: '/core/api/w/jobs/routes/switch/' + jobKey + '/' + to,
    method: 'post',
  })
}

////////////////////////////////数据字典//////////////////////////////////////
export const ApiOfDictionaryList = (dictionaryType) => {
  return fetch({
    url: '/core/api/w/dictionaries/' + dictionaryType,
    method: 'get',
  })
}

////////////////////////////////@DataBinder钩子函数//////////////////////////////////////
export const pageableResponseFormatter = (responseHandler, res, originResponse) => {
  res = {
    success: res.status,
    message: '',
    data: {
      data: res.data,
      pageable: res.pageable
    }
  };
  responseHandler(res, originResponse);
}

export const defaultBindingData = {
  data: [],
  pageable: {
    first: false,
    fromNumber: 0,
    last: false,
    number: 0,
    numberOfElements: 0,
    size: 10,
    toNumber: 0,
    totalElements: 0,
    totalPages: 0,
  }
}

export const responseFormatter = (responseHandler, res, originResponse) => {
  res = {
    success: res.status,
    data: {
      "list": res.data
    }
  };
  responseHandler(res, originResponse);
}

export const dataBinderError = (res, defaultCallback, originResponse) => {
  //
}
